import pyaudio
import numpy as np

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100

silent_threshold = 2000  # 静音状态的阈值，根据实际情况调整
sudden_increase_threshold = 2000  # 突然声音增加的阈值，根据实际情况调整

p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

previous_amplitude = 0
while True:
    data = stream.read(CHUNK)
    audio_data = np.frombuffer(data, dtype=np.int16)
    amplitude = np.abs(audio_data).mean()

    if amplitude < silent_threshold:
        previous_amplitude = amplitude
    elif amplitude > previous_amplitude + sudden_increase_threshold:
        print("检测到可能的汽车报警声，发出通知！")
        # 这里可以添加发送通知的代码，比如发送邮件、调用其他通知程序等

stream.stop_stream()
stream.close()
p.terminate()